package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;

import com.bjesc_b2c.app.appraiset.bean.PutTouristsBean;
import com.bjesc_b2c.app.appraiset.bean.TouristsBean;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.CustomerModel;
import com.google.gson.Gson;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/5/24.
 */

public class CustomerModelImpl implements CustomerModel {
    private CallBack callBack;
    public CustomerModelImpl(CallBack callBack){
        this.callBack = callBack;
    }    @Override
    public void requestCustomer(Context context,String id,int type) {
        HashMap<String, Object> build = new MapUtil()
                .put("dispatch_id", id)
                .put("type", type)
                .build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).getCusinfo(),
                new BaseSubscriber<TokenBean<TouristsBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean<TouristsBean> o) {
                        if(callBack!=null){
                            callBack.callback(o.getData());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        if(callBack!=null){
                            callBack.loadFailure();
                        }
                    }

                    @Override
                    public void onStart() {
                        if(callBack!=null){
                            callBack.loadStart();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        if(callBack!=null){
                            callBack.loadCompleted();
                        }
                    }
                });
    }

    @Override
    public void modifyCustimerInfo(Context context, String dis_id, PutTouristsBean infoBean, int type) {
        HashMap<String, Object> build = new MapUtil()
                .put("data", new Gson().toJson(infoBean))
                .put("dispatch_id",dis_id)
                .put("type", type)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).modifyCusinfo(),
                new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean to) {
                        callBack.onSuccess(to);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                    }
                });
    }

    @Override
    public void createOrder(Context context, String dis_id, PutTouristsBean infoBean) {
        HashMap<String, Object> build = new MapUtil()
                .put("data", new Gson().toJson(infoBean))
                .put("dispatch_id",dis_id)
                .build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).createOrder(),
                new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean to) {
                        if(callBack!=null){
                            callBack.onSuccess(to);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        if(callBack!=null){
                            callBack.loadFailure();
                        }
                    }

                    @Override
                    public void onStart() {
                        if(callBack!=null){
                            callBack.loadStart();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        if(callBack!=null){
                            callBack.loadCompleted();
                        }
                    }
                });
    }

    public interface CallBack{
        void callback(TouristsBean bean);
        void loadStart();
        void loadCompleted();
        void loadFailure();
        void onSuccess(TokenBean bean);
    }}
